﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExpertMIS.Brokers;
using System.Collections;
using ExpertMIS.Entities;
using System.Text;

namespace ExpertMIS.Admin
{
    public partial class UserAuditList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            pgvUsers.DeleteClientScript = "if(!confirm('您确定要删除该用户吗？')) return false; ";
            pgvUsers.EditClientURL = "~/Admin/UserAuditForm.aspx?oid={0}";
            pgvUsers.AuditClientURL = "~/Admin/UserAuditList.aspx?oid={0}&action=audit&passed={1}";
            pgvUsers.DeleteClientURL = "~/Admin/UserAuditList.aspx?oid={0}&action=delete";
            pgvUsers.ObjectName = "ExpertMIS.UserAuditListView";
            pgvUsers.PK = "F_OID";
            pgvUsers.CheckFieldName = "F_IsPassed";
            pgvUsers.IsLoadData = true;

            if (!IsPostBack)
            {
                //// 这里需要再次判断，如果为客户端脚本制定window.location.href,则IsPostBack也为false
                //if (string.IsNullOrEmpty(Request["search"]))
                //{
                //    // 页面初次加载，则不需要load数据
                //    pgvUsers.IsLoadData = false;
                //}
            }

            string action = Request["action"];
            if (!string.IsNullOrEmpty(action))
            {
                string oid = Request["oid"];
                string passed = Request["passed"];
                switch (action)
                {
                    case "audit":
                        bool bCheck = false;
                        if (passed.ToString() == "1" || passed.ToString().ToLower() == "true")
                        {
                            bCheck = true;
                        }
                        AuditUsers(oid as string, bCheck);
                        break;
                    case "delete":
                        DeleteUser(oid);
                        break;
                    default:
                        break;
                }
            }
        }

        /// <summary>
        /// 审核用户
        /// </summary>
        /// <param name="OID"></param>
        /// <param name="checkedValue"></param>
        protected void AuditUsers(string OID, bool checkedValue)
        {
            try
            {
                ADMUserBroker broker = new ADMUserBroker();
                Hashtable hsQuery = new Hashtable();
                hsQuery.Add("OID", OID);
                ADMUserEntity entity = broker.GetEntity(hsQuery) as ADMUserEntity;

                if (entity == null)
                {
                    return;
                }

                entity.IsPassed = !checkedValue;
                broker.SaveEntity(entity, null);
            }
            catch
            {
                return;
            }
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="OID"></param>
        protected void DeleteUser(string OID)
        {
            try
            {
                ADMUserBroker broker = new ADMUserBroker();
                Hashtable hsQuery = new Hashtable();
                hsQuery.Add("OID", OID);
                ADMUserEntity entity = broker.GetEntity(hsQuery) as ADMUserEntity;

                if (entity == null)
                {
                    return;
                }
                broker.RemoveEntity(entity, null);
            }
            catch
            {
                return;
            }
        }

        public string GetStartHiddenFieldClientID()
        {
            return this.pgvUsers.GetStartHiddenFieldClientID();
        }
    }
}
